package com.sum.common.utils;

import android.text.TextUtils;
import android.util.Log;
import android.util.Printer;

public class LogU {
    private static final String TAG = LogU.class.getSimpleName();
    private static final int V = 1;
    private static final int D = 2;
    private static final int I = 3;
    private static final int W = 4;
    private static final int E = 5;
    private static boolean SHOW_LOG_FLAG = true;  //是否显示log日志
    private static Printer FILE_PRINTER;
    private static final int MAX_TIME = 1000 * 60 * 30; // half hour
    public static boolean SAVE_LOG_TO_FILE_FLAG;

    public static void init() {
        SHOW_LOG_FLAG = true;
    }

    public static void v(Object obj) {
        log(V, null, obj);
    }

    public static void v(String tag, Object obj) {
        log(V, tag, obj);
    }

    public static void d(Object obj) {
        log(D, null, obj);
    }

    public static void d(String tag, Object obj) {
        log(D, tag, obj);
    }

    public static void i(Object obj) {
        log(I, null, obj);
    }

    public static void i(String tag, Object obj) {
        log(I, tag, obj);
    }

    public static void w(Object obj) {
        log(W, null, obj);
    }

    public static void w(String tag, Object obj) {
        log(W, tag, obj);
    }

    public static void w(String tag, String msg, Object obj) {
        log(W, tag + "[" + msg + "]", obj);
    }

    public static void e(Object obj) {
        log(E, null, obj);
    }

    public static void e(String tag, Object obj) {
        log(E, tag, obj);
    }

    public static void e(String tag, String msg, Object obj) {
        log(E, tag + "[" + msg + "]", obj);
    }

    public static void stack() {
        logStack(null);
    }

    public static void stack(String tag) {
        logStack(tag);
    }

    private static void logStack(String tag) {
        if (SHOW_LOG_FLAG) {
            StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();
            int index = 4;
            String className = stackTrace[index].getFileName();//获取类名
            tag = TextUtils.isEmpty(tag) ? className : tag;

            StringBuilder sb = new StringBuilder("Log stack trace\n");
            sb.append("=======================================================================================================================================================")
                    .append("\n");
            for (; index < stackTrace.length; index++) {
                sb.append("|| ").append(stackTrace[index]).append("\n");
            }
            sb.append("=======================================================================================================================================================");
            Log.d(tag, sb.toString());
        }
    }

    public static void log(int type, String tagStr, Object obj) {
        StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();

        int index = 4;
        String className = stackTrace[index].getFileName();//获取类名
        String methodName = stackTrace[index].getMethodName();//获取方法名
        int lineNumber = stackTrace[index].getLineNumber();//获取代码行数

        String tag = (tagStr == null ? className : tagStr);

        StringBuilder sb = new StringBuilder();
        sb.append("[(").append(className).append(":").append(lineNumber).append(") #").append(methodName).append("]");

        if (obj == null) {
            sb.append("Log with null object");
        } else if (obj instanceof Throwable) {
            sb.append("Throwable! Print stack trace--------------\n");
            sb.append(Log.getStackTraceString((Throwable) obj));
        } else {
            sb.append(obj.toString());
        }
        String logStr = sb.toString();
        switch (type) {
            case V:
                Log.v(tag, logStr);
                break;
            case D:
                Log.d(tag, logStr);
                break;
            case I:
                Log.i(tag, logStr);
                break;
            case W:
                Log.w(tag, logStr);
                break;
            case E:
                Log.e(tag, logStr);
                break;
        }
    }
}